/**
 * routes/deviceBorrow.js
 */
var express = require("express");
var router = express.Router();
//导入所需控制器
const deviceBorrow = require("../controller/borrowController");
const auth = require("../controller/auth");


/**
 * @swagger
 * components:
 *   schemas:
 *     DeviceBorrow:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 *         deviceId:
 *           type: integer
 *         userId:
 *           type: integer
 *         borrowDate:
 *           type: string
 *           format: date-time
 *         returnDate:
 *           type: string
 *           format: date-time
 *           nullable: true
 *         status:
 *           type: string
 *           enum: [borrowed, returned]
 *         createdAt:
 *           type: string
 *           format: date-time
 *         updatedAt:
 *           type: string
 *           format: date-time
 */

/**
 * @swagger
 * tags:
 *   name: DeviceBorrows
 *   description: 设备借还管理
 */

/**
 * @swagger
 * /deuse/borrows:
 *   post:
 *     summary: 预约设备
 *     tags: [DeviceBorrows]
 *     description: 预约设备，需要设备ID、开始时间、结束时间
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - deviceId
 *               - startTime
 *               - endTime
 *             properties:
 *               deviceId:
 *                 type: integer
 *               startTime:
 *                 type: string
 *                 format: date-time
 *               endTime:
 *                 type: string
 *                 format: date-time
 *               purpose:
 *                 type: string
 *     responses:
 *       201:
 *         description: 预约申请已提交，等待审核
 *       400:
 *         description: 参数错误或设备已被预约
 *       500:
 *         description: 服务器错误
 */
router.post("/borrows", auth.isLogin, deviceBorrow.add);

/**
 * @swagger
 * /deuse/borrows/status:
 *   put:
 *     summary: 更新预约状态（管理员）
 *     tags: [DeviceBorrows]
 *     description: 管理员审核预约申请
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - deviceId
 *               - status
 *             properties:
 *               deviceId:
 *                 type: integer
 *               status:
 *                 type: string
 *                 enum: [approved, rejected]
 *               remarks:
 *                 type: string
 *     responses:
 *       200:
 *         description: 状态更新成功
 *       400:
 *         description: 参数错误
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器错误
 */
router.put("/borrows/status", auth.isLogin, auth.isAdmin, deviceBorrow.update);

/**
 * @swagger
 * /deuse/borrows/return:
 *   put:
 *     summary: 归还设备
 *     tags: [DeviceBorrows]
 *     description: 归还已借用的设备
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - deviceId
 *             properties:
 *               deviceId:
 *                 type: integer
 *     responses:
 *       200:
 *         description: 归还成功
 *       400:
 *         description: 设备未借用或归还失败
 *       500:
 *         description: 服务器错误
 */
router.put("/borrows/return", auth.isLogin, deviceBorrow.update);

/**
 * @swagger
 * /deuse/borrows:
 *   get:
 *     summary: 获取预约记录列表
 *     tags: [DeviceBorrows]
 *     description: 获取预约记录列表，管理员可查看所有记录，普通用户只能查看自己的记录
 *     parameters:
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, approved, rejected, completed]
 *         description: 过滤预约状态
 *       - in: query
 *         name: userId
 *         schema:
 *           type: integer
 *         description: 按用户ID过滤（仅管理员可用）
 *       - in: query
 *         name: deviceId
 *         schema:
 *           type: integer
 *         description: 按设备ID过滤
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页条数
 *       - in: query
 *         name: offset
 *         schema:
 *           type: integer
 *           default: 0
 *         description: 偏移量
 *     responses:
 *       200:
 *         description: 返回预约记录列表
 *       500:
 *         description: 服务器错误
 */
router.get("/borrows", auth.isLogin, deviceBorrow.findAll);

/**
 * @swagger
 * /deuse/borrows/{bookingId}:
 *   delete:
 *     summary: 删除预约记录
 *     tags: [DeviceBorrows]
 *     description: 根据预约ID删除预约记录，只有预约用户或管理员才能删除。
 *     parameters:
 *       - in: path
 *         name: bookingId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 预约记录ID
 *     responses:
 *       204:
 *         description: 删除成功
 *       400:
 *         description: 参数错误
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器错误
 */
router.delete("/borrows/:id", auth.isLogin, deviceBorrow.remove);

/**
 * @swagger
 * /deuse/borrows/overdue:
 *   get:
 *     summary: 获取超期未归还的设备列表
 *     tags: [DeviceBorrows]
 *     description: 获取所有超期未归还的设备列表（仅管理员可用）
 *     responses:
 *       200:
 *         description: 返回超期设备列表
 *       403:
 *         description: 权限不足
 *       500:
 *         description: 服务器错误
 */
router.get("/borrows/overdue", auth.isLogin, auth.isAdmin, deviceBorrow.getOverdueDevices);

/**
 * @swagger
 * /deuse/borrows/return:
 *   post:
 *     summary: 归还设备
 *     tags: [DeviceBorrows]
 *     description: 归还已借用的设备
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - deviceId
 *             properties:
 *               deviceId:
 *                 type: integer
 *               userId:
 *                 type: integer
 *                 description: 用户ID（管理员归还时需要）
 *               returnNote:
 *                 type: string
 *                 description: 归还备注
 *     responses:
 *       200:
 *         description: 归还成功
 *       400:
 *         description: 设备未借用或归还失败
 *       500:
 *         description: 服务器错误
 */
router.post("/borrows/return", auth.isLogin, deviceBorrow.returnDevice);

/**
 * @swagger
 * /deuse/borrows/cancel:
 *   post:
 *     summary: 取消预约
 *     tags: [DeviceBorrows]
 *     description: 取消待审核的预约
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - deviceId
 *             properties:
 *               deviceId:
 *                 type: integer
 *               cancelReason:
 *                 type: string
 *                 description: 取消原因
 *     responses:
 *       200:
 *         description: 取消成功
 *       400:
 *         description: 预约不存在或已开始
 *       500:
 *         description: 服务器错误
 */
router.post("/borrows/cancel", auth.isLogin, deviceBorrow.cancelBooking);

module.exports = router;
